﻿Friend Class CSharpRegexFormatter
    Inherits ModelBase
    Implements IRegexFormatter

    Public Function Format(regex As String, options As System.Text.RegularExpressions.RegexOptions) As String Implements IRegexFormatter.Format
        Dim l_expressionBuilder As New Text.StringBuilder()

        Dim l_namespace As String = "System.Text.RegularExpressions"
        Dim l_class As String = "Regex"
        Dim l_optionsEnum As String = "RegexOptions"

        l_expressionBuilder.Append(String.Format("{0}.{1} regex = new {0}.{1}", l_namespace, l_class))

        l_expressionBuilder.Append("(")

        Dim l_regexLines = From l_regexPart In regex.Split(Environment.NewLine)
                           Select String.Format("@{0}{1}{0}", ControlChars.Quote, l_regexPart.Replace(ControlChars.Quote, ControlChars.Quote & ControlChars.Quote))

        l_expressionBuilder.AppendLine(
            String.Join(" +" & Environment.NewLine, l_regexLines.ToArray())
        )

        If options > RegexOptions.None Then

            Dim l_optionNames =
                From l_regexOption In [Enum].GetValues(GetType(RegexOptions))
                Where (options And l_regexOption) > 0
                Select String.Format("{0}.{1}.{2}", l_namespace, l_optionsEnum, [Enum].GetName(GetType(RegexOptions), l_regexOption))

            Dim l_optionsList = String.Join(" | ", l_optionNames.ToArray())

            l_expressionBuilder.Append(", " & l_optionsList)

        End If

        l_expressionBuilder.Append(");")

        Return l_expressionBuilder.ToString()
    End Function

End Class
